package com.coder4j.auth.controller;

import com.coder4j.auth.entity.AuthApiEntity;
import com.coder4j.auth.service.AuthApiService;
import com.coder4j.auth.vo.AuthApiQuery;
import com.coder4j.auth.vo.AuthApiVO;
import com.coder4j.auth.vo.AuthApiEO;
import com.coder4j.starter.web.GroupOptionVO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 接口
 * @author keller
 * @date 2024-05-07 18:10:10
 */
@Slf4j
@RestController
@RequestMapping("/authApi")
public class AuthApiController {
    @Resource
    private AuthApiService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询接口
     * @param id AuthApi.id
     * @return 接口对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询接口",createName = "keller",createTime = "2024-05-07 18:10:10")
    public R<AuthApiVO> getById( Long id){

        log.info("====== AuthApiControllerGetById ====== {}",id);
        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存接口
     * 有 id 字段，更新；否则新增
     * @param authApi AuthApi对象
     * @return AuthApi对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存接口",createName = "keller",createTime = "2024-05-07 18:10:10")
    public R save(@RequestBody AuthApiVO authApi){
        log.info("====== AuthApiControllerSave ====== {}",authApi);

        if(ObjectUtils.hasEmpty(authApi.getName(),authApi.getRequestMethod(),authApi.getAppName(),authApi.getControllerName(),authApi.getMethodName(),authApi.getUrl(),authApi.getSignatureCode())){
            return R.paramError();
        }

        service.baseSaveVO(authApi);
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询接口列表",createName = "keller",createTime = "2024-05-07 18:10:10")
    public R<BasePage<AuthApiVO>> getList(@RequestBody AuthApiQuery query){
        if(query == null){
            query = new AuthApiQuery();
        }
        log.info("====== AuthApiControllerGetList ====== {}",query);
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出接口列表",createName = "keller",createTime = "2024-05-07 18:10:10")
    public void export(@RequestBody AuthApiQuery query){
        if(query == null){
            query = new AuthApiQuery();
        }
        log.info("====== AuthApiControllerGetList ====== {}",query);
        List<AuthApiEntity> list = service.baseQueryList(query);
                List<AuthApiEO> eoList = new ArrayList<>();
                for (AuthApiEntity entity : list) {
                    AuthApiEO eo = new AuthApiEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"AuthApiList",eoList);
    }


    /**
     * 删除接口
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id AuthApi.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除接口",createName = "keller",createTime = "2024-05-07 18:10:10")
    public R deleteById( Long id){
        log.info("====== AuthApiControllerDeleteById ======  {}",id);
        service.baseDeleteById(id);
        return R.success();
    }

    @GetMapping("/getGroupOptions")
    @ApiNote(name="获取接口的分组选择项",createName = "keller",createTime = "2024-05-07 18:10:10")
    public R getTreeSelector(){
        List<GroupOptionVO> list =  service.getGroupOptions();
        return R.success(list);
    }
}
